﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TIS.DAO;
using System.Data;

namespace TIS.Administrator
{
    public partial class ManagerSlideShow : System.Web.UI.Page
    {
        LanguageDAO _languageDao = new LanguageDAO();
        SlideShowDAO _slideShowDao = new SlideShowDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["langId"] == null)
                {
                    Session["langId"] = 1;
                }

                GetDataDdlLanguage();
                loadGridView();
                loadForm();
            }
        }
        public void loadForm()
        {
             if (Request.QueryString["id"] != null)
             {
                 string id = Request.QueryString["id"].ToString();
                 DataTable dt = _slideShowDao.GetSlideShow(int.Parse(id), int.Parse(Session["langId"].ToString()));
                 foreach (DataRow row in dt.Rows)
                 {
                     txtPosition.Text = row["SlideShowPosition"].ToString();
                     txtURL.Text = row["SlideShowURL"].ToString();
                     if (row["SlideShowStatus"].ToString().Equals("1"))
                     {
                         rdoStatusDisplay.Checked = true;
                         rdoStatusHidden.Checked = false;
                     }
                     else
                     {
                         rdoStatusDisplay.Checked = false;
                         rdoStatusHidden.Checked = true;
                     }
                 }

                
             }
            
        }

        public void loadGridView()
        {
            gridViewSlideShow.DataSource = _slideShowDao.GetSlideShowAll(int.Parse(Session["langId"].ToString()));
            gridViewSlideShow.DataBind();
        }
        public void GetDataDdlLanguage()
        {

            ddlLanguage.DataSource = _languageDao.GetAllLanguage();
            ddlLanguage.DataTextField = "LanguageName";
            ddlLanguage.DataValueField = "LanguageId";
            ddlLanguage.SelectedValue = Session["langId"].ToString();
            ddlLanguage.DataBind();
        }

        protected void btnShowLanguage_Click(object sender, EventArgs e)
        {
            Session["langId"] = ddlLanguage.SelectedValue;
            Response.Redirect("ManagerSlideShow.aspx");
        }

        protected void lbtnRemove_Command(object sender, CommandEventArgs e)
        {
            _slideShowDao.DeleteSlideShow(int.Parse(e.CommandArgument.ToString()), int.Parse(Session["langId"].ToString()));
            Response.Redirect("ManagerSlideShow.aspx");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string imgSmall = "";
            int status;
            if (rdoStatusDisplay.Checked)
            {
                status = 1;
            }
            else
            {
                status = 0;
            }
            if (Request.QueryString["id"] == null)
            {
                if (fileUpload.FileName != "")
                {
                    imgSmall = "/Upload/Image/" + fileUpload.FileName;
                    fileUpload.SaveAs(Server.MapPath("~/Upload/Image/") + fileUpload.FileName);
                }

                _slideShowDao.InsertSlideShow(imgSmall, int.Parse(txtPosition.Text), txtURL.Text, int.Parse(Session["langId"].ToString()), status);
               
                Response.Redirect("ManagerSlideShow.aspx");
            }
            else
            {
                string id = Request.QueryString["id"].ToString();
                if (fileUpload.FileName != "")
                {
                    imgSmall = "/Upload/Image/" + fileUpload.FileName;
                    fileUpload.SaveAs(Server.MapPath("~/Upload/Image/") + fileUpload.FileName);
                }
                _slideShowDao.UpdateSlideShow(int.Parse(id),imgSmall, int.Parse(txtPosition.Text), txtURL.Text, int.Parse(Session["langId"].ToString()), status);
                Response.Redirect("ManagerSlideShow.aspx");
            }
        }

        protected void lbtnEdit_Command(object sender, CommandEventArgs e)
        {
            Response.Redirect("ManagerSlideShow.aspx?id="+e.CommandArgument.ToString());
        }
    }
}